package com.jsb.laundry.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.jsb.laundry.common.response.BaseRequestParam;
import com.jsb.laundry.common.response.WebResponse;
import com.jsb.laundry.common.utils.JSBUtil;
import com.jsb.laundry.service.OrderSplittingServiceImpl;
import com.jsb.laundry.service.ServeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = {"理约金相关"})
@RestController
@RequestMapping("/serve")
public class ServeController {
    private static final Logger LOGGER = LoggerFactory.getLogger(ServeController.class);
    @Resource
    private ServeService serveService;

    @Resource
    private OrderSplittingServiceImpl orderSplittingService;


    @ApiOperation(value = "客户下单分佣", notes = "客户下单分佣", httpMethod = "POST")
    @PostMapping("agencyCommission")
    public void agencyCommission(@RequestBody JSONObject object) {
        LOGGER.info("data{}",object);
        String orderId = object.getString("orderId");
        String riderId = object.getString("riderId");
        String ledgerType = object.getString("ledgerType");
        orderSplittingService.agencyCommission(orderId, riderId, ledgerType);
    }

    @ApiOperation(value = "查询服务", notes = "查询服务", httpMethod = "POST")
    @PostMapping("/findServiceById")
    public WebResponse findServiceById(@RequestBody String data) {
        JSONObject jsonObject = JSONObject.parseObject(data);
        String id = jsonObject.getString("id");
        String userId = jsonObject.getString("userId");
        if (JSBUtil.isEmptyString(id)) {
            return WebResponse.failOfParameter();
        }
        Map res = serveService.findServiceById(id);
        int num = serveService.findByUserIdOkFlag(userId);
        res.put("is_ok_pay_service", num);
        Object price = res.get("price");
        res.put("price", JSBUtil.turnDoubleStr(price.toString()));
        LOGGER.info("查询服务,请求参数:{},返回数据：{}", id, res);
        return WebResponse.successData(res);
    }

    @ApiOperation(value = "查看开通服务", notes = "查看开通服务", httpMethod = "POST")
    @PostMapping("/findOpenServiceLogByUserId")
    public WebResponse findOpenServiceLogByUserId(@RequestBody BaseRequestParam data) {
        if (JSBUtil.isEmptyString(data.getParam())) {
            return WebResponse.failOfParameter();
        }
        PageHelper.startPage(data.getPageNum(), data.getPageSize());
        List<HashMap> res = serveService.findOpenServiceLogByUserId(data.getParam());
        LOGGER.info("查看开通服务,请求参数:{},返回数据：{}", data.getParam(), res);
        return WebResponse.successData(res);
    }

}
